<template>
  <view>
    <view class="good-list">
      <view v-for="(item,i) in goodsList" :key="i" @click="gotoDetail(item)">
        <my-goods :item="item"></my-goods>
      </view> 
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 商品列表
        goodsList:[],
        total:0,
        
        // 请求对象
        queryObj:{
          // 请求关键词
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10,
          
          // 节流阀
          isloding:false
        }
      };
    },
    onLoad(options){
      this.queryObj.query = options.query||''
      this.queryObj.cid = options.cid||''
      
      this.getGoodList()
      // console.log(this.queryObj);
      // 请求数据
      
    },
    methods:{
      // 获取商品列表
      async getGoodList(cb){
        // 打开节流阀
        this.isloding = true
        const { data:res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        // 关闭节流阀
        this.isloding = false
        // 如果cb存在,那就调用
        cb && cb()
        if(res.meta.status !== 200) return uni.$http.showMsg()    
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      // 导航到详情页面
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      }
    },
    onReachBottom() {
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
      // 节流阀判断不重复请求数据
      if(this.isloding === true) return
      // 自增加+1
      this.queryObj.pagenum ++
      this.getGoodList()
    },
    onPullDownRefresh() {
      // 重置数据
      this.queryObj.pagenum = 1
      this.queryObj.total = 0
      this.isloding = false
      this.goodsList = []
      // 重新发起数据请求 回调函数
      this.getGoodList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
